/*
 * Copyright 2018 devemux86
 *
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.xiaoxiao.source;

import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.OverzoomTileDataSource;
import org.oscim.tiling.source.LwHttp;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.mapzen.TileDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SMapVectorTileSource extends UrlTileSource {
    // http://smap.navinfo.com/gateway/vector_map_world/smap-data/data/vector?specId=902168&ak=740171084c904ebc7915d284c&dataSource=Road&z=10&x=841&y=387
    private static final String DEFAULT_URL = "http://smap.navinfo.com/gateway/vector_map_world/smap-data/data/vector";
    private static final String DEFAULT_PATH = "z={Z}&x={X}&y={Y}";
    private String dataSourceValue = null;

    static final Logger log = LoggerFactory.getLogger(LwHttp.class);

    public static class Builder<T extends Builder<T>> extends UrlTileSource.Builder<T> {
        private String locale = "";
        private String dataSource = null;

        public Builder() {
            super(DEFAULT_URL, DEFAULT_PATH);
            keyName("ak");
            apiKey("740171084c904ebc7915d284c");
            overZoom(15);
        }

        public T locale(String locale) {
            this.locale = locale;
            return self();
        }

        public T dataSource(String dataSource){
            this.dataSource = dataSource;
            return self();
        }
        @Override
        public SMapVectorTileSource build() {
            return new SMapVectorTileSource(this);
        }
    }

    private String locale = "";
    private String dataSource = null;
    protected SMapVectorTileSource(Builder<?> builder) {
        super(builder);
        this.locale = builder.locale;
        this.dataSource = builder.dataSource;
    }

    @SuppressWarnings("rawtypes")
    public static Builder<?> builder() {
        return new Builder();
    }

    /**
     * Create BitmapTileSource for 'url'
     * <p/>
     * By default path will be formatted as: url/z/x/y.png
     * Use e.g. setExtension(".jpg") to overide ending or
     * implement getUrlString() for custom formatting.
     */
    public SMapVectorTileSource(String url, int zoomMin, int zoomMax) {
        this(DEFAULT_URL, DEFAULT_PATH, zoomMin, zoomMax);
    }

    public SMapVectorTileSource(String url, int zoomMin, int zoomMax, String extension) {
        this(DEFAULT_URL, DEFAULT_PATH + extension, zoomMin, zoomMax);
    }

    public SMapVectorTileSource(String url, String tilePath, int zoomMin, int zoomMax) {
        super(builder()
                .url(url)
                .tilePath(tilePath)
                .zoomMin(zoomMin)
                .zoomMax(zoomMax));
    }

    @Override
    public ITileDataSource getDataSource() {
        return new OverzoomTileDataSource(new UrlTileDataSource(this, new SMapTileDecoder(locale), getHttpEngine()), 22);
    }

    public String getTileUrl(Tile tile) {
        StringBuilder sb = new StringBuilder();
        StringBuilder urlSB=sb.append(getUrl()).append("?").append(URL_FORMATTER.formatTilePath(this, tile));
        sb.append("&").append("ak").append("=").append("740171084c904ebc7915d284c");
        urlSB.append("&").append("specId").append("=").append("902168");
        if (this.dataSource!=null) {
            urlSB.append("&").append("dataSource").append("=").append(this.dataSource);
        }
        return urlSB.toString();
    }
}
